%% The 'switch' statement
%
% Similar to 'if' but useful when working with character strings or vectors of different
% lengths

% Example:

a='asdf'

switch a
    case 'dheke'
        fprintf('found dheke\n')
    case 'asdf'
        fprintf('found asdf\n')
    otherwise
        fprintf('found neither\n')
end

% 'help switch' for more info


%% Constructors and Destructors

clear tc
clear classes

% See our 'TextCon' class definition
tc=TestCon()

clear tc
%Note the text written to the command window



%% Figures are objects (Starting in MATLAB 2014b)

x=(1:10)
y=x.^2

figO=figure()
pO=plot(x,y)

% 'figO' and 'pO' are objects.  Check out thier properties and methods

%% Draw vectors on a plot

x=0
y=0
u=0
v=1

figure
quiver(x,y,u,v,0)
%see 'help quiver' for the various ways to use 'quiver'

%set our own desired range for the axis
axis([-3 3 0 3])


%% Making a movie

%Create figure
fH=figure

% See 'help VideoWriter' for options and example

filename='test.mp4'
VidObj=VideoWriter(filename,'MPEG-4')
% Note 'VidObj' is an object of class 'VideoWriter'

%Set FrameRate property
VidObj.FrameRate=1

%call 'open' method
VidObj.open()

for ii=1:10
    figure(fH) %make 'fH' the current figure
    v=ii/10;
    quiver(x,y,u,v,0)   %make plot with something changing. 
    axis([-3 3 0 3])
    currFrame = getframe();
    VidObj.writeVideo(currFrame);
end

VidObj.close() %don't forget to close!

%% A mass on a spring

m = 10;      %kg
k = 100;    %N/m
A = .1;     %m
t0=0;       %s
y0=-1;      %m

%make vectors:
dt=.05;
%this is the period
T=2*pi/(sqrt(k/m))
t=(0:dt:2*T);
Yposition=A*sin(sqrt(k/m)*(t-t0))+y0;
Yvelocity=sqrt(k/m)*A*cos(sqrt(k/m)*(t-t0));
Yforce=-k*(Yposition-y0);

%make figure
fH=figure;

%Setup movie
filename='MassOnSpring.mp4'
VidObj=VideoWriter(filename,'MPEG-4')

%Set FrameRate property
VidObj.FrameRate=1/dt

%Open video file for writing
VidObj.open();

for ii=1:length(t)
    figure(fH) %make 'fH' the current figure
    %Plot the position of the mass
    plot(0,Yposition(ii),'ko','linewidth',5)
    axis([-1 1 -3 1])
    hold on %so that other plots will add to existing plot
    
    %Represent spring as gray line
    plot([0 0],[0 Yposition(ii)],'Color',[0.5 0.5 0.5],'linewidth',5)
    
    %Draw velocity vector
    quiver(0,Yposition(ii),0,Yvelocity(ii),0,'g','linewidth',4)
    
    %Draw Force vector
    quiver(0,Yposition(ii),0,Yforce(ii)/10,0,'r','linewidth',3)
    
    %Draw y0
    plot([-1 1],[y0 y0],'k--')
    
    hold off %so new plot will won't add to this one
    
    xlabel('X(m)')
    ylabel('Y(m)')
    title('Mass on a spring')
    legend('Mass','Spring','Velocity (m/s)','Force (10*N)')
    
    %now add frame to movie
    currFrame = getframe();
    VidObj.writeVideo(currFrame);
    
end

%close video file
VidObj.close() 































